package com.example.ibsp_demo.controller.create;

import com.example.ibsp_demo.mapper.content.DraftMapper;
import com.example.ibsp_demo.pojo.entity.content.Draft;
import com.example.ibsp_demo.pojo.response.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/draft")
@CrossOrigin(origins = "http://localhost:8080")
public class DraftController {

    @Autowired
    private DraftMapper draftMapper;

    // 保存草稿
    @PostMapping("/save")
    public ResponseMessage<Map<String, Object>> saveDraft(@RequestBody Draft draft){
        // 基础参数校验（保持不变）
        if (draft.getUid() == null) {
            return ResponseMessage.error(1001, "用户ID不能为空");
        }
        if (draft.getTitle() == null || draft.getTitle().trim().isEmpty()) {
            return ResponseMessage.error(1002, "标题不能为空");
        }
        if (draft.getContentText() == null || draft.getContentText().trim().isEmpty()) {
            return ResponseMessage.error(1003, "正文内容不能为空");
        }

        draft.setLastEditTime(new Date());
        draft.setIsSubmit(0); // 保存时重置为未提交（可选）

        Integer generatedDraftId;
        if (draft.getDraftId() == null) {
            // 无draftId：新增草稿
            draftMapper.insert(draft);
            generatedDraftId = draft.getDraftId();
            if (generatedDraftId == null) {
                return ResponseMessage.error(1004, "草稿保存失败，未生成ID");
            }
        } else {

            int rows = draftMapper.update(draft);
            if (rows == 0) {
                return ResponseMessage.error(1005, "草稿不存在或未修改");
            }
            generatedDraftId = draft.getDraftId(); // 直接使用现有ID
        }

        // 返回统一格式（使用Map避免类型问题）
        Map<String, Object> data = new HashMap<>();
        data.put("draftId", generatedDraftId);
        data.put("message", draft.getDraftId() == null ? "草稿创建成功" : "草稿更新成功");
        return ResponseMessage.success(data);
    }

    @DeleteMapping("/delete")
    public ResponseMessage<String> deleteDraft(@RequestParam Integer draftId) {
        try {
            Draft draft = draftMapper.selectByDraftId(draftId);
            if (draft == null) {
                return ResponseMessage.error(1005, "草稿不存在");
            }
            draftMapper.deleteByDraftId(draftId);
            return ResponseMessage.success("草稿删除成功"); // 正确返回成功状态
        } catch (Exception e) {
            return ResponseMessage.error(500, "服务器错误: " + e.getMessage());
        }
    }

    @GetMapping("/detail")
    public ResponseMessage<Draft> getDraftDetail(@RequestParam Integer draftId) {
        Draft draft = draftMapper.selectByDraftId(draftId);
        if (draft == null) {
            return ResponseMessage.error(1005, "草稿不存在");
        }
        return ResponseMessage.success(draft);
    }

    // 查询用户的所有草稿
    @GetMapping("/list")
    public ResponseMessage<List<Draft>> getDraftsByUid(@RequestParam Integer uid) {
        List<Draft> drafts = draftMapper.selectByUid(Map.of("uid", uid));
        return ResponseMessage.success(drafts);
    }


}
